﻿using CreateCodeByDatabase.Dal;
using CreateCodeByDatabase.Model;
using Dapper;
using ReadModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CreateCodeByDatabase.Bll
{
    public class Operate_Mysql : IOperate
    {
        /// <summary>
        /// 根据合约号查询园机入库信息
        /// </summary>
        /// <param name="hyh">合约号</param>
        /// <returns></returns>
        public override IEnumerable<TableInfo> QueryTableList(string tableName)
        {
            string sql = "select table_name TableName,table_comment Comment from information_schema.tables where table_schema='gysdjcs_zs'";
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                if (tableName.IndexOf("_") > -1)
                    sql += " and Upper(table_name) like '%" + tableName.Replace("_", "\\_").ToUpper() + "%' escape'\\'";
                else
                    sql += " and Upper(table_name) like '%" + tableName.ToUpper() + "%'";
            }
            sql += " order by table_name";

            using (DapperHelper help = new DapperHelper(enumConnectionType.MySql))
            {
                try
                {
                    return help.DbContext.Query<TableInfo>(sql);
                }
                catch (Exception ex)
                {
                    Log.WriteLog(ex);
                    return null;
                }
            }
        }
        //public override IEnumerable<TableInfo> QueryTableList1()
        //{
        //    string sql = "select table_name TableName,table_comment Comment from information_schema.tables where table_schema='gysdjcs'";
        //    using (DapperHelper help = new DapperHelper(enumConnectionType.Oracle))
        //    {
        //        try
        //        {
        //            IDataReader reader = help.DbContext.ExecuteReader(sql);
        //            Type T = typeof(TableInfo);
        //            var entityList = Query<TableInfo>(reader).ToList();
        //            return entityList;
        //        }
        //        catch (Exception ex)
        //        {
        //            Log.WriteLog(ex);
        //            return null;
        //        }
        //    }
        //}
        //public override IEnumerable<TableInfo> QueryTableList2()
        //{
        //    string sql = "select table_name TableName,table_comment Comment from information_schema.tables where table_schema='gysdjcs'";
        //    using (DapperHelper help = new DapperHelper(enumConnectionType.Oracle))
        //    {
        //        try
        //        {
        //            IDataReader reader = help.DbContext.ExecuteReader(sql);
        //            Type T = typeof(TableInfo);
        //            var entityList = ReflectionTest.GetEntityList<TableInfo>(reader).ToList();
        //            return entityList;
        //        }
        //        catch (Exception ex)
        //        {
        //            Log.WriteLog(ex);
        //            return null;
        //        }
        //    }
        //}

        /// <summary>
        /// 获取指定表列信息
        /// </summary>
        /// <param name="tableNameList">指定表名集合</param>
        /// <returns></returns>
        public override IEnumerable<ColumnInfo> GetTableColumnList(IEnumerable<string> tableNameList)
        {
            string sqlTemplate = "select table_name TableName, COLUMN_NAME ColumnName,DATA_TYPE DataType,COLUMN_COMMENT Comment from information_schema.COLUMNS where table_name = '{0}' order by ordinal_position  ";
            using (DapperHelper help = new DapperHelper(enumConnectionType.MySql))
            {
                string sql = null;
                try
                {
                    List<ColumnInfo> entityList = new List<ColumnInfo>();
                    foreach (string item in tableNameList)
                    {
                        sql = string.Format(sqlTemplate, item);
                        entityList.AddRange(help.DbContext.Query<ColumnInfo>(sql));
                    }
                    return entityList;
                }
                catch (Exception ex)
                {
                    Log.WriteLog(ex);
                    return null;
                }
            }
        }
        public override int ExecuteSql(string sql, List<string> aloneExecuteSqlList = null)
        {
            if (string.IsNullOrEmpty(sql))
                return 0;
            using (DapperHelper help = new DapperHelper(enumConnectionType.MySql))
            {
                try
                {
                    help.DbContext.Execute(sql);
                    if (aloneExecuteSqlList != null && aloneExecuteSqlList.Count > 0)
                    {
                        foreach (var item in aloneExecuteSqlList)
                        {
                            help.DbContext.Execute(item);
                        }
                    }
                    return 1;
                }
                catch (Exception ex)
                {
                    Log.WriteLog(ex);
                    return 0;
                }
            }
        }
    }
}
